using System;
using System.IO;
using System.Numerics;
using UnityEngine;

namespace LS.Digitize
{
    public sealed class BigintegerSerializer: CompoundSerializer<BigInteger>
    {
        public BigintegerSerializer() : base(typeof(BigInteger),typeof(byte[]))
        {
            
        }
        
        public override void WriteData(IWriter writer, BigInteger obj)
        {
            ISerializer subSerializer = SubSerializer;
            if (subSerializer is PrimitiveSerializer<byte[]> ps)
                ps.WriteData(writer, obj.ToByteArray());
            else
                subSerializer.Write(writer, obj.ToByteArray());
        }

        public override BigInteger ReadData(IReader reader)
        {
            ISerializer subSerializer = SubSerializer;
            if (subSerializer is PrimitiveSerializer<byte[]> ps)
                return new BigInteger(ps.ReadData(reader));
            else
                return new BigInteger((byte[])subSerializer.Read(reader));
        }
        
        public override void ReadInto(IReader reader, ref BigInteger obj)
        {
            obj = ReadData(reader);
        }
    }
}